home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / windows.pyc (.txt) < prev   
Python Compiled Bytecode  |  2014-12-31  |  7KB  |  231 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import os
  5. import collections
  6. import ctypes
  7. if os.name == 'nt':
  8.     from win32com.shell import shellcon, shell
  9.     import pywintypes
  10.     import pythoncom
  11. SHGFP_TYPE_CURRENT = 0
  12. SHGFP_TYPE_DEFAULT = 1
  13. CSIDL_FLAG_CREATE = 32768
  14. CSIDL_FLAG_DONT_UNEXPAND = 8192
  15.  
  16. def _get_path(folder, default = False, create = False):
  17.     '''A path to an directory or None'''
  18.     if default:
  19.         flags = SHGFP_TYPE_DEFAULT
  20.     else:
  21.         flags = SHGFP_TYPE_CURRENT
  22.     if create:
  23.         folder |= CSIDL_FLAG_CREATE
  24.     folder |= CSIDL_FLAG_DONT_UNEXPAND
  25.     
  26.     try:
  27.         path = shell.SHGetFolderPath(0, folder, 0, flags)
  28.     except pywintypes.com_error:
  29.         return None
  30.  
  31.     if not isinstance(path, unicode):
  32.         path = path.decode('ascii')
  33.     return path
  34.  
  35.  
  36. def get_personal_dir(**kwargs):
  37.     return _get_path(shellcon.CSIDL_PERSONAL, **kwargs)
  38.  
  39.  
  40. def get_appdate_dir(**kwargs):
  41.     return _get_path(shellcon.CSIDL_APPDATA, **kwargs)
  42.  
  43.  
  44. def get_desktop_dir(**kwargs):
  45.     return _get_path(shellcon.CSIDL_DESKTOP, **kwargs)
  46.  
  47.  
  48. def get_music_dir(**kwargs):
  49.     return _get_path(shellcon.CSIDL_MYMUSIC, **kwargs)
  50.  
  51.  
  52. def get_profile_dir(**kwargs):
  53.     return _get_path(shellcon.CSIDL_PROFILE, **kwargs)
  54.  
  55.  
  56. def get_link_target(path):
  57.     '''Takes a path to a .lnk file and returns a path the .lnk file
  58.     is targeting or None.
  59.     '''
  60.     link = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
  61.     
  62.     try:
  63.         link.QueryInterface(pythoncom.IID_IPersistFile).Load(path)
  64.         path = link.GetPath(0)[0]
  65.         return path.decode('latin-1')
  66.     except pywintypes.com_error:
  67.         pass
  68.  
  69.  
  70.  
  71. def get_links_dir():
  72.     '''Get the path to the Links directory (%USERPROFILE%\\Links) or None'''
  73.     
  74.     try:
  75.         kfm = pythoncom.CoCreateInstance(shell.CLSID_KnownFolderManager, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IKnownFolderManager)
  76.     except pywintypes.com_error:
  77.         return None
  78.  
  79.     
  80.     try:
  81.         libs_folder = kfm.GetFolder(shell.FOLDERID_Links)
  82.         return libs_folder.GetPath()
  83.     except pywintypes.com_error:
  84.         pass
  85.  
  86.  
  87. if os.name == 'nt':
  88.     SetEnvironmentVariableW = ctypes.windll.kernel32.SetEnvironmentVariableW
  89.     SetEnvironmentVariableW.argtypes = [
  90.         ctypes.c_wchar_p,
  91.         ctypes.c_wchar_p]
  92.     SetEnvironmentVariableW.restype = ctypes.c_bool
  93.     GetEnvironmentStringsW = ctypes.windll.kernel32.GetEnvironmentStringsW
  94.     GetEnvironmentStringsW.argtypes = []
  95.     GetEnvironmentStringsW.restype = ctypes.c_void_p
  96.     FreeEnvironmentStringsW = ctypes.windll.kernel32.FreeEnvironmentStringsW
  97.     FreeEnvironmentStringsW.argtypes = [
  98.         ctypes.c_void_p]
  99.     FreeEnvironmentStringsW.restype = ctypes.c_bool
  100.  
  101. class WindowsEnvironError(Exception):
  102.     pass
  103.  
  104.  
  105. def _set_windows_env_var(key, value):
  106.     '''Set an env var.
  107.  
  108.     Can raise WindowsEnvironError
  109.     '''
  110.     if not isinstance(key, unicode):
  111.         raise TypeError
  112.     if not isinstance(value, unicode):
  113.         raise TypeError
  114.     status = SetEnvironmentVariableW(key, value)
  115.     if status == 0:
  116.         raise WindowsEnvironError
  117.  
  118.  
  119. def _del_windows_env_var(key):
  120.     '''Delete an env var.
  121.  
  122.     Can raise WindowsEnvironError
  123.     '''
  124.     if not isinstance(key, unicode):
  125.         raise TypeError
  126.     status = SetEnvironmentVariableW(key, None)
  127.     if status == 0:
  128.         raise WindowsEnvironError
  129.  
  130.  
  131. def _get_windows_environ():
  132.     '''Returns a unicode dict of the Windows environment.
  133.  
  134.     Can raise WindowsEnvironError
  135.     '''
  136.     res = GetEnvironmentStringsW()
  137.     if not res:
  138.         raise WindowsEnvironError
  139.     res = ctypes.cast(res, ctypes.POINTER(ctypes.c_wchar))
  140.     done = []
  141.     current = u''
  142.     i = 0
  143.     while None:
  144.         c = res[i]
  145.         i += 1
  146.         if c == u'\x00':
  147.             if not current:
  148.                 break
  149.             done.append(current)
  150.             current = u''
  151.             continue
  152.         current += c
  153.         continue
  154.         dict_ = { }
  155.         for entry in done:
  156.             
  157.             try:
  158.                 (key, value) = entry.split(u'=', 1)
  159.             except ValueError:
  160.                 continue
  161.  
  162.             dict_[key] = value
  163.         
  164.     status = FreeEnvironmentStringsW(res)
  165.     if status == 0:
  166.         raise WindowsEnvironError
  167.     return dict_
  168.  
  169.  
  170. class WindowsEnviron(collections.MutableMapping):
  171.     '''os.environ that supports unicode on Windows.
  172.  
  173.     Keys can either be ascii bytes or unicode
  174.  
  175.     Like os.environ it will only contain the environment content present at
  176.     load time. Changes will be synced with the real environment.
  177.     '''
  178.     
  179.     def __init__(self):
  180.         
  181.         try:
  182.             env = _get_windows_environ()
  183.         except WindowsEnvironError:
  184.             env = { }
  185.  
  186.         self._env = env
  187.  
  188.     
  189.     def __getitem__(self, key):
  190.         if isinstance(key, bytes):
  191.             key = key.decode('ascii')
  192.         return self._env[key]
  193.  
  194.     
  195.     def __setitem__(self, key, value):
  196.         if isinstance(key, bytes):
  197.             key = key.decode('ascii')
  198.         
  199.         try:
  200.             _set_windows_env_var(key, value)
  201.         except WindowsEnvironError:
  202.             pass
  203.  
  204.         self._env[key] = value
  205.  
  206.     
  207.     def __delitem__(self, key):
  208.         if isinstance(key, bytes):
  209.             key = key.decode('ascii')
  210.         
  211.         try:
  212.             _del_windows_env_var(key)
  213.         except WindowsEnvironError:
  214.             pass
  215.  
  216.         del self._env[key]
  217.  
  218.     
  219.     def __iter__(self):
  220.         return iter(self._env)
  221.  
  222.     
  223.     def __len__(self):
  224.         return len(self._env)
  225.  
  226.     
  227.     def __repr__(self):
  228.         return repr(self._env)
  229.  
  230.  
  231.